package com.tanhua.dubbo.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.exception.BusinessException;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.pojo.HuanXinUser;
import com.tanhua.dubbo.server.vo.HuanXinUserVo;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: HuanXinService
 * @Author:王晨晗
 * @Description:
 * @Date: 2022/01/05/18:59
 */
@Service
public class HuanXinService {
    @Reference
    private HuanXinApi huanXinApi;

    /**
     * @description 查询当前用户的环信用户信息;
     * @return
     */
    public HuanXinUserVo queryHuanXinUser() {
        User user = UserThreadLocal.get();
        //查询当前用户的环信账号信息
        HuanXinUser huanXinUser = huanXinApi.queryHuanXinUser(user.getId());
        //判断是否为空, 不为空 ,封装对象返回客户端结果;
        if (ObjectUtil.isNotEmpty(huanXinUser)){
            return new HuanXinUserVo(huanXinUser.getUsername() , huanXinUser.getPassword());
        }
        //为空,抛出异常;
        throw new BusinessException(ErrorResult.huanxinUserError());  // 获取环信用户信息失败
    }
}
